
"""
@author : sn
@date : 2017-6-26
@description : this script is a tcp proxy , the param receive_first mian
@usage : python tcp_proxy.py [local_host] [local_port] [remote_host] [remote_port] [receive_first]
@usage_example : python tcp_proxy.py 0.0.0.0 9000 10.1.1.1 9000 True
"""

import sys
import socket
import threading


def server_loop(local_host, local_port, remote_host, remote_port, receive_first):
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        server.bind((local_host, local_port))
    except:
        print "[!!] Failed to listen on %s:%d" % (local_host, local_port)
        print "[!!] Check for other listening sockets or correct permissions."
        sys.exit(0)

    print "[*] Listening on %s:%d" % (local_host, local_port)

    server.listen(5)

    while True:
        client_socket, addr = server.accept()

        # print the message from local connection
        print "[==>] Received incoming connection from %s:%d" % (addr[0], addr[1])

        # open a thread to change data with remote computer
        proxy_thread = threading.THread(target=proxy_handler,
                                        args=(client_socket, remote_host, remote_port, receive_first))
        proxy_thread.start()


# handle server received data
def proxy_handler(client_socket, remote_host, remote_port, receive_first):
    # connect to remote compute
    remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    remote_socket.connect((remote_host, remote_port))

    if receive_first:
        remote_buffer = receive_from(remote_socket)
        hexdump(remote_buffer)

        # send to response handle
        remote_buffer = response_handler(remote_buffer)

        if len(remote_buffer):
            print "[<==] Sending %d bytes to localhost." % len(remote_buffer)
            client_socket.send(remote_buffer)

        # read data from localhost and send to remote
        while True:
            # read data from localhost
            local_buffer = receive_from(client_socket)

            if len(local_buffer):
                print "[==>] Received %d bytes from localhost." % len(local_buffer)
                hexdump(local_buffer)

                # send to request handler
                local_buffer = request_handler(local_buffer)

                # send to remote
                remote_socket.send(local_buffer)
                print "[==>] Send to remote."

            remote_buffer = receive_from(remote_socket)

            if len(remote_buffer):
                print "[<===] Received %d from remote." % len(remote_buffer)
                hexdump(remote_buffer)

                # send to response handler
                remote_buffer = response_handler(remote_buffer)

                # send to localhost
                client_socket.send(remote_buffer)

                print "[<==] Send to localhost"

            if not len(local_buffer) or not len(remote_buffer):
                client_socket.close()
                remote_socket.close()
                print "[*} No more data. Closing connections."
                break


# this method is change string to hex code
def hexdump(src, length=16):
    result = []
    digits = 4 if isinstance(src, unicode) else 2

    for i in xrange(0, len(src), length):
        s = src[i:i+length]
        hexa = b''.join(["%0*X" % (digits, ord(x)) for x in s])
        text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.' for x in s])
        result.append(b"%04X  %-*s  %s" % (i, length*(digits+1), hexa, text))

    print b'\n'.join(result)


def receive_from(connection):
    buffer = ""

    connection.settimeout(2)

    try:
        while True:
            data = connection.recv(4096)
            if not data:
                break
            buffer += data
    except:
        pass
    return buffer


def request_handler(buffer):
    return buffer


def response_handler(buffer):
    return buffer


def main():
    if len(sys.argv[1:]) != 5:
        print "Usage: ./proxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]"
        print "Example : ./proxy.py 127.0.0.1 9000 10.12.132.1 9000 True"
        sys.exit(0)

    # set local listening params
    local_host = sys.argv[1]
    local_port = int(sys.argv[2])

    # set remote params
    remote_host = sys.argv[3]
    remote_port = int(sys.argv[4])

    receive_first = sys.argv[5]
    if "True" in receive_first:
        receive_first = True
    else:
        receive_first = False

    server_loop(local_host, local_port, remote_host, remote_port, receive_first)

main()
